
package com.hlkj.pay.vo.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.exception.ErrorCode;
import com.hlkj.framework.common.exception.IErrorCode;
import com.hlkj.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.hlkj.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.util.Assert;

import javax.validation.constraints.NotEmpty;
import java.util.Objects;

/**
 * @author HlpayTeam
 * @date 2024/11/08 9:06
 */
@Data
public class OpenApiResult<T> extends CommonResult<T> {
    @Schema(description = "appid")
    private String appId;
    @Schema(description = "商户号")
    private String sn;
    @Schema(description = "签名")
    private String sign;
    @Schema(description = "时间戳")
    @NotEmpty(message = "时间戳")
    private String timestamp;
    @Schema(description = "签名方式，目前只支持RSA2")
    private String signType;
    public static <T> OpenApiResult<T> error(Integer code, String message) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的！");
        OpenApiResult<T> result = new OpenApiResult<>();
        result.setCode(code);
        result.setMsg(message);
        return result;
    }
    
    public static <T> OpenApiResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }
    
    public static <T> OpenApiResult<T> error(IErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }
    
    public static <T> OpenApiResult<T> success(T data) {
        OpenApiResult<T> result = new OpenApiResult<>();
        result.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        result.setData(data);
        result.setMsg(GlobalErrorCodeConstants.SUCCESS.getMsg());
        return result;
    }
    public static <T> OpenApiResult<T> successApi(CommonResult<T> data) {
        OpenApiResult<T> result = new OpenApiResult<>();
        result.setCode(data.getCode());
        result.setData(data.getData());
        result.setMsg(data.getMsg());
        return result;
    }
    
    public static <T> OpenApiResult<T> success() {
        OpenApiResult<T> result = new OpenApiResult<>();
        result.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        result.setMsg(GlobalErrorCodeConstants.SUCCESS.getMsg());
        result.setData(null);
        return result;
    }
    public static boolean isSuccess(Integer code) {
        return Objects.equals(code, GlobalErrorCodeConstants.SUCCESS.getCode());
    }
}
